<?php

namespace app\api\controller\v1\store\base;

use app\admin\model\DataStoreAlbum;
use app\admin\model\DataStoreUser;
use app\api\controller\Api;

class Album extends Api
{
    public function get(){
        $list = $this->getModel()->where(['unid'=>$this->user['id']])->findOrEmpty();
        if($list->isEmpty())$this->error('相册不存在');
        $this->success('获取成功',$list);
    }

    public function set(){

        $rule = [
            'title'    => 'require',
        ];
        $message  =   [
            'title.require' => '相册名不为空',
        ];
        $this->validate($this->post, $rule,$message);
        $store = DataStoreUser::where(['unid'=>$this->user['id'],'status'=>1])->findOrEmpty();
        if($store->isEmpty())$this->error('商铺不存在或未通过审核');
        $this->post['unid'] = $this->user['id'];
        $this->post['store_id'] = $store->id;

        $list = DataStoreAlbum::create($this->post);
        if($list){
            $this->success('操作成功',$list);
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 批量删除相册
     * @return void
     */
    public function remove(){
        $rule = [
            'album_id'    => 'require',
        ];
        $message  =   [
            'album_id.require' => '相册不为空',
        ];
        $this->validate($this->post, $rule,$message);
        $album = explode(',',$this->post['album_id']);
        $success =0;
        foreach ($album as $value){
            $delete = $this->getModel()->findOrEmpty($value);
            if(!$delete->isEmpty()){
                $delete_save= $delete->delete();
                if($delete_save)$success++;
            }

        }
        $this->success('删除成功'.$success.'个');

    }


    private function getModel(){
        return new DataStoreAlbum();
    }

}